<template>
  <div class="meetingresults">
    <div class="header" v-if="viewType!='approval'">
      <p style="margin-right: 30px"><strong>项目编号：</strong>{{ projectBuildMeetVo.projectNo }}</p>
      <p><strong>项目名称：</strong>{{ projectBuildMeetVo.projectName }}</p>
    </div>
    <div class="content">
      <div class="title">
        <strong>参与人</strong>
      </div>
      <ul class="participants-list">
        <li>
          <span>预计上会主管：</span>
          <span>{{ projectBuildMeetVo.projectBuildMeet.estimateMeetPerson }}</span>
        </li>
        <li>
          <span>主要汇报人：</span>
          <span>{{ projectBuildMeetVo.projectBuildMeet.mainRemitter }}</span>
        </li>
        <li>
          <span>参与上会人：</span>
          <span>{{ projectBuildMeetVo.projectBuildMeet.joinRemitter }}</span>
        </li>
      </ul>
    </div>
    <div class="content">
      <div class="title">
        <strong>上会结果</strong>
      </div>
      <a-table
        :columns="columns"
        :data-source="projectBuildMeetVo.nodeMeetList"
        bordered
        style="padding-bottom: 40px; border-bottom: 1px solid #f0f0f0"
        :pagination="false"
      >
        <template slot="toEdit" slot-scope="text, record">
          <a-button type="primary" size="small" @click="toEdit(record)" :disabled="!canEdit">编辑</a-button>
        </template>
      </a-table>
    </div>
    <div class="content">
      <div class="title">
        <strong>上会附件</strong>
      </div>
      <div class="download-control">
        <ht-upload
          ref="otherAttachs"
          class="upload-demo"
          :disabled="!canEdit"
          multiple
          dbKey="Trd"
          busField="ProjectBuild"
          :busKey="projectBuildId"
          busType="proMeetResult"
          ftpKey="ftp"
          v-model="otherAttachList"  
          :show-file-list="false"
        >
          <el-button size="small" type="primary" style="height: 30px" :disabled="!canEdit">
            <span style="font-size: 14px">上传</span></el-button
          >
        </ht-upload>
        <a-button class="default" style="margin-left:10px;" @click="deleteBatch" :disabled="!canEdit"><a-icon type="delete"/>删除</a-button>
        <a-button class="default" @click="downLoadBatch" :disabled="!canEdit"><a-icon type="download"/>下载</a-button>
      </div>
      <a-table
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :columns="Attachcolumns"
        :data-source="otherAttachDataList"
        bordered
        class="uploadAttach"
        :pagination="false"
      >
      <template slot="name" slot-scope="text, record">
        <a @click="showAttach(record.key, record.uploader, record.name, true)">{{ record.name }}</a>
      </template>
      </a-table>
    </div>
    <project-build-meet v-if="meet.flag" :meet="meet.entity" :only-show="meet.onlyShow" @close="initData()" />
  </div>
</template>
<script>

import $ from 'jquery'
import '@/utils/upload.install.js'
import { getAction, postAction,previewAttach } from '@/api/manage'
import Vue from 'vue'
import { ACCESS_TOKEN, USER_INFO } from '@/store/mutation-types'
import { formatFileSize, endWith, strNotNull } from '@/utils/common.js'

const columns = [
  {
    title: '上会节点',
    dataIndex: 'timeNodeCode',
    width: '10%',
  },
  {
    title: '实际上会日期',
    dataIndex: 'realityMeetDate',
    align: 'center',
    width: '12%',
  },
  {
    title: '评审结果是否同意',
    dataIndex: 'meetPassed',
    width: '14%',
    align: 'center',
  },
  {
    title: '评审级别',
    dataIndex: 'proType',
    width: '9%',
    align: 'center',
  },
  {
    title: '评审绩点',
    dataIndex: 'proPoint',
    width: '9%',
  },
  {
    title: '评审核心力',
    dataIndex: 'meetAssessReward',
    width: '10%',
  },
  {
    title: '评审意见汇总',
    dataIndex: 'opinion',
    width: '35%',
  },
  { title: '操作', dataIndex: 'id', width: '10%', scopedSlots: { customRender: 'toEdit' } },
]

export default {
  props:{
    viewType:{
      Type: String,
      default: ''
    }
  },
  components: {
    ProjectBuildMeet: () => import('@/components/tools/projectBuildStup/ProjectBuildMeet'),
  },
  watch: {
    otherAttachList: function () {
      this.otherAttachDataListFresh()
    },
  },
  data() {
    return {
      columns,
      meet: {
        flag: false,
        entity: {},
        onlyShow: false,
      },
      projectBuildId: this.$route.query.projectBuildId,
      selectedRowKeys: [],
      otherAttachList: [],
      otherAttachDataList: [],
      url: '/projectBuild/projectBuildMeet/getProjectBuildMeetVO',
      loading: false,
      projectBuildMeetVo: {
        projectBuildMeet: {},
        nodeMeetList: [],
      },
      Attachcolumns: [
        {
          title: '上会节点',
          dataIndex: 'meetNode',
          align: 'center',
          width: '10%',
        },
        {
          title: '附件名称',
          dataIndex: 'name',
          scopedSlots: { customRender: 'name' },
        },
        {
          title: '附件大小',
          dataIndex: 'size',
          width: '15%',
        },
        {
          title: '上传人',
          dataIndex: 'uploaderName',
          width: '20%',
        },
        {
          title: '上传时间',
          dataIndex: 'time',
          width: '20%',
        },
      ],
      attach: {
        // getProjectBuildVOWithProjectManagers: '/projectBuild/getProjectBuildVOWithProjectManagers',
        getProjectBuildAttachCodeInfos: '/projectBuild/getProjectBuildAttachCodeInfos',
        downLoadBatch: '/common/downLoadBatch',
        deleteBatch: '/common/deleteBatch',
      },
      canEdit: false
    }
  },
  mounted() {
    this.initData()
  },
  computed: {
  },
  methods: {
    initData() {
      this.meet.flag = false
      getAction(this.url, { projectBuildId: this.projectBuildId })
        .then((res) => {
          if (res.code == 200) {
            console.log(res.result)
            this.projectBuildMeetVo = res.result
            var currUser = Vue.ls.get(USER_INFO).username
            if (this.projectBuildMeetVo.createUserId === currUser || this.projectBuildMeetVo.currentAuditUserId === currUser
                 || (this.projectBuildMeetVo.projectManagers!=null && this.projectBuildMeetVo.projectManagers.indexOf(currUser)>-1)
                 || (this.projectBuildMeetVo.assistantProjectManagers!=null && this.projectBuildMeetVo.assistantProjectManagers.indexOf(currUser)>-1)
                 ) {
                   this.canEdit = true;
                }
            if (this.projectBuildMeetVo.nodeMeetList != null) {
              this.projectBuildMeetVo.nodeMeetList.forEach((meet) => {
                meet.key = meet.id
              })
            }
            if(this.projectBuildMeetVo.projectBuildMeet==null){this.projectBuildMeetVo.projectBuildMeet={}}
            if(this.projectBuildMeetVo.nodeMeetList==null){this.projectBuildMeetVo.nodeMeetList=[]}
          } else {
            this.$message.warning('查询上会信息失败:' + res.message)
          }
        })
        .catch((res) => {
          this.$message.warning('查询上会信息失败:' + res.message)
        })
    },
    showAttach(id, uploader, fileName, autoDownload) {
      if (!this.canEdit) {
        this.$message.warning('仅有该附件上传人、项目经理、副经理可以查看!')
        return
      }
      //以rar、zip、dwg结尾的文件，直接下载不预览
      if (
        endWith(fileName, 'rar') ||
        endWith(fileName, 'zip') ||
        endWith(fileName, 'dwg') ||
        endWith(fileName, 'tgz')
      ) {
        if (autoDownload) {
          this.doDownLoadBatch(id)
          return
        }
        this.$message.warning('该附件类型不支持预览，请下载附件查看')
        return
      }
      let params={
        id:id,
        ftpKey:'ftp'
      };
      previewAttach(params).then(result=>{
        const url=URL.createObjectURL(result)
        if (this.projectBuildMeetVo.projectClass == '40' || this.projectBuildMeetVo.projectClass == '61') {
          window.open(process.env.BASE_URL+'/pdf/web/normalViewer.html?file=' + url)
        } else {
          window.open(process.env.BASE_URL+'/pdf/web/viewer.html?file=' + url)
        }
      }).catch(err => {
        this.$message.warning('下载文件失败');
      })
    },
    doDownLoadBatch(attachIds) {
      if (document.getElementById('_attachToolView_downloadfile_frame') == null) {
        var downloadFrame = $('<iframe id="_attachToolView_downloadfile_frame" style="display: none"></iframe>')
        downloadFrame.appendTo($('body'))
      }
      var iframe = document.getElementById('_attachToolView_downloadfile_frame')
      var href =
        this.API_BASE_URL +
        this.attach.downLoadBatch +
        '?attachIds=' +
        attachIds +
        '&dbKey=Trd&ftpKey=ftp&token=' +
        Vue.ls.get(ACCESS_TOKEN)
      iframe.src = href
    },
    toEdit(record) {
      this.meet.flag = true
      this.meet.entity = record
    },
    downLoadBatch() {
      if(this.selectedRowKeys==null || this.selectedRowKeys.length<1){
        this.$message.warning('请选择相应附件');
        return;
      }
      if (document.getElementById('_attachToolView_downloadfile_frame') == null) {
        var downloadFrame = $('<iframe id="_attachToolView_downloadfile_frame" style="display: none"></iframe>')
        downloadFrame.appendTo($('body'))
      }
      var iframe = document.getElementById('_attachToolView_downloadfile_frame')
      var href =
        this.API_BASE_URL +
        this.attach.downLoadBatch +
        '?attachIds=' +
        this.selectedRowKeys +
        '&dbKey=Trd&ftpKey=ftp&token=' +
        Vue.ls.get(ACCESS_TOKEN)
      iframe.src = href
    },
    deleteBatch(){
      if(this.selectedRowKeys==null || this.selectedRowKeys.length<1){
        this.$message.warning('请选择相应附件');
        return;
      }
      getAction(this.attach.deleteBatch, { attachIds: this.selectedRowKeys.join(','), dbKey: 'Trd', ftpKey: 'ftp' })
        .then((res) => {
          if (res.code == 1) {
            this.$message.success('批量删除成功')
            setTimeout(function () {
              window.location.reload()
            }, 500)
          } else {
            this.$message.warning('批量删除失败:' + res.msg)
          }
        })
        .catch((res) => {
          this.$message.warning('批量删除失败:' + res.msg)
        })
    },
    onSelectChange(selectedRowKeys) {
      console.log('selectedRowKeys changed: ', selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
    },
    otherAttachDataListFresh() {
      this.otherAttachDataList = []
      this.otherAttachList.forEach((item) => {
        var size = formatFileSize(item.size)
        this.otherAttachDataList.push({
          meetNode: '立项上会',
          key: item.id,
          name: item.fileName,
          size: size,
          time: item.uploadTime,
          uploader: item.uploader,
          uploaderName: item.uploaderName,
        })
      })
    },
  },
}
</script>
<style lang="less" scoped>
/deep/ .ant-btn-primary {
  background: #4095e4;
}
/deep/ .ant-table-tbody .ant-input {
  border-radius: unset;
  border: unset;
}
/deep/.ant-table-tbody .ant-table-row td {
  padding-top: 10px;
  padding-bottom: 10px;
}
/deep/ .ant-btn {
  height: 30px;
  border-radius: 2px;
  margin-right: 10px;
}
/deep/ .ant-table-thead > tr > th {
  background: #dadee9 !important;
}
.uploadAttach /deep/ .ant-table-tbody .ant-table-row td {
  &:nth-child(3) {
    color: #3e8ee2;
  }
}
/deep/.ant-table-thead > tr:first-child > th:first-child,
/deep/ .ant-table table {
  border-radius: unset;
  line-height: 1;
}
.meetingresults {
  border: 1px solid #e6e6e6;
  background-color: #fff;
  .header {
    border-bottom: 1px solid #e6e6e6;
    display: flex;
    padding: 30px 0 0 25px;
  }
  .content {
    padding: 30px 25px;
    .title {
      border-left: 3px solid #47a4fa;
      padding: 0 10px;
      margin-bottom: 20px;
    }
    .download-control {
      display: flex;
      margin-right: 10px;
      margin-bottom: 10px;
    }
    .participants-list {
      padding: 0;
      margin: 0;
      list-style: none;
      li {
        margin-bottom: 20px;
        span {
          display: inline-block;
          margin-left: 10px;
        }
      }
    }
  }
  .btn {
    display: flex;
    justify-content: center;
    margin: 30px 0;
  }
}
</style>